package cn.lili.modules.member.serviceimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lili.common.constants.Constants;
import cn.lili.common.security.context.UserContext;
import cn.lili.modules.member.entity.dos.MemberEvaluationThumbs;
import cn.lili.modules.member.entity.dto.EvaluationThumbsParams;
import cn.lili.modules.member.mapper.MemberEvaluationThumbsMapper;
import cn.lili.modules.member.service.MemberEvaluationThumbsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 评价点赞记录实现类
 *
 * @author moxingjian
 * @version 1.0
 * @date 2023-12-05 11:44
 * <p>
 * 版本更新：
 * 更新版本号    更新人      更新时间      更新内容
 */
@Service
public class MemberEvaluationThumbsServiceImpl extends ServiceImpl<MemberEvaluationThumbsMapper, MemberEvaluationThumbs> implements MemberEvaluationThumbsService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int handleByThumpsUpFlag(String memberEvaluationId, Boolean thumpsUpFlag) {
        String memberId = UserContext.getCurrentUser().getId();
        // 先查询之前是否有记录
        EvaluationThumbsParams evaluationThumbsParams = new EvaluationThumbsParams();
        evaluationThumbsParams.setEvaluationId(memberEvaluationId);
        evaluationThumbsParams.setMemberId(memberId);
        List<MemberEvaluationThumbs> evaluationThumbsParamsList = this.listByParameter(evaluationThumbsParams);
        int count = 0;
        if (CollUtil.isNotEmpty(evaluationThumbsParamsList)) {
            MemberEvaluationThumbs evaluationThumbs = evaluationThumbsParamsList.get(0);
            // 删除
            this.removeBatchByIds(evaluationThumbsParamsList);
            // 如果赞成标志不一致，则数量+1
            if (!thumpsUpFlag.equals(evaluationThumbs.getThumbsUpFlag())) {
                count = 1;
            } else {
                // 如果赞成标志一致，只做删除操作
                return count;
            }
        } else {
            // 直接新增 + 1
            count = 1;
        }
        // 再保存到记录表中
        MemberEvaluationThumbs evaluationThumbs = new MemberEvaluationThumbs();
        evaluationThumbs.setEvaluationId(memberEvaluationId);
        evaluationThumbs.setMemberId(memberId);
        evaluationThumbs.setThumbsUpFlag(thumpsUpFlag);
        this.save(evaluationThumbs);
        return count;
    }

    /**
     * 参数查询
     * @param evaluationThumbsParams 查询参数
     * @return 查询结果
     */
    @Override
    public List<MemberEvaluationThumbs> listByParameter(EvaluationThumbsParams evaluationThumbsParams) {
        return this.list(getQueryWrapper(evaluationThumbsParams));
    }

    private LambdaQueryWrapper<MemberEvaluationThumbs> getQueryWrapper(EvaluationThumbsParams evaluationThumbsParams) {
        List<String> evaluationIds = new ArrayList<>();
        if (CharSequenceUtil.isNotBlank(evaluationThumbsParams.getEvaluationIds())) {
            evaluationIds = Arrays.stream(evaluationThumbsParams.getEvaluationIds().split(Constants.COMMA)).toList();
        }
        return Wrappers.<MemberEvaluationThumbs>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(evaluationThumbsParams.getMemberId()),
                        MemberEvaluationThumbs::getMemberId, evaluationThumbsParams.getMemberId())
                .eq(ObjectUtil.isNotEmpty(evaluationThumbsParams.getEvaluationId()),
                        MemberEvaluationThumbs::getEvaluationId, evaluationThumbsParams.getEvaluationId())
                .eq(ObjectUtil.isNotEmpty(evaluationThumbsParams.getThumbsUpFlag()),
                        MemberEvaluationThumbs::getThumbsUpFlag, evaluationThumbsParams.getThumbsUpFlag())
                .in(CollUtil.isNotEmpty(evaluationIds), MemberEvaluationThumbs::getEvaluationId, evaluationIds);
    }

}
